package com.cy.device.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * MQ消息发送者
 *
 * @author: jiangwang
 * @created: 2023/10/24 11:08
 */
@Component
public class RabbitmqProducer {

    private final Logger logger = LoggerFactory.getLogger(RabbitmqProducer.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String routingKey, String message) {
        try {
            rabbitTemplate.convertAndSend(routingKey, message);
            logger.info("向MQ发送消息：{}", message);
        } catch (Exception e) {
            logger.error("发送数据到MQ出错：", e);
        }
    }
}
